﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Speciality : IEquatable<Speciality>
    {
        public void ImportList(List<Classes.Speciality> specList)
        {
            var mdb = new MedregDBDataContext();
            var ListSpec = new List<Speciality>();
            foreach (var spec in specList)
            {
                Speciality spec1 = new Speciality() { ID = spec.ID, Name = spec.Name, Parent = spec.Parent};
                Speciality query;

                if (mdb.Speciality.LongCount() == 0)
                {
                    mdb.Speciality.InsertOnSubmit(spec1);
                }
                else
                {
                    try
                    {
                        query = mdb.Speciality.Single(l => l.ID == spec1.ID);
                        query.Name = spec1.Name;

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {

                        if (!ListSpec.Contains(spec1))
                        {
                            mdb.Speciality.InsertOnSubmit(spec1);
                            ListSpec.Add(spec1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(Speciality other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
